
import { login, getUserInfo, getUserDetailById } from '@/api/user'
// import { getToken, setToken } from '@/utils/auth'
const state = {
  // 两种方式存取数据
  token: localStorage.getItem('token') || '',
  userInfo: {}
  // token: getToken() || ''

}
const mutations = {
  setToken(state, payload) {
    state.token = payload

    localStorage.setItem('token', payload)
    // setToken(payload)

    // 主动监听超时
    const now = Date.now()
    localStorage.setItem('time', now)
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  logout(state) {
    state.token = ''
    state.userInfo = {}
  }
}
const actions = {
  async login(context, payload) {
    // console.log('封装发送请求')
    const res = await login(payload)
    // console.log(res)

    context.commit('setToken', res)
  },
  async getUserInfo(context) {
    const res = await getUserInfo()
    // console.log(res)

    const detail = await getUserDetailById(res.userId)
    // console.log('员工的基本信息', detail)
    const userInfo = {
      ...res,
      ...detail
    }
    // console.log(userInfo)
    context.commit('setUserInfo', userInfo)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

